const VIC = `赢啦!`;
const FAIL = `输掉了...`;
const BALANCE = `打平了`;
const AISpeed = 300; //机器人出拳延迟
const handlerDis = 16; //监听延迟
const vm = new Vue({
    el: `#app`,
    data: {
        gameLock: 0,
        transitionEnd: 0,
        playerOpt: 0,
        AIOpt: 0,
        res: {
            '-2': VIC,
            '-1': FAIL,
            '0': BALANCE,
            '1': VIC,
            '2': FAIL
        },
        gameItems: [
            { id: 1, name: `剪刀`, src: `./img/scissors.png` },
            { id: 2, name: `石头`, src: `./img/stone.png` },
            { di: 3, name: `布`, src: `./img/cloth.png` }
        ]
    },
    methods: {
        getRandNum: function(l, r) {
            //获取一个[l,r]之间的随机整数的方法
            return Math.floor(Math.random() * (r - l + 1) + l);
        },
        getAIChoice: function() { //获取机器人的出拳
            const NUM = 10; //机器人进行10次随机取值，选择最后一次结果作为自己的猜拳结果
            let count = 0,
                tempVal = 0,
                lastVal;
            const timeId = setInterval(() => {
                if (count < NUM) {
                    lastVal = tempVal;
                    do {
                        tempVal = this.getRandNum(0, 2);
                    } while (lastVal === tempVal); //为了视觉效果，只展示与上次出拳结果不一样的结果
                    count++;
                    this.AIOpt = tempVal
                } else {
                    clearInterval(timeId);
                    this.transitionEnd = 1;
                }
            }, AISpeed);
        },
        winOrLose: function() { //判断胜负
            alert(`机器人出的是【${this.gameItems[this.AIOpt].name}】,你出的是【${this.gameItems[this.playerOpt].name}】结果是：你${this.res[this.playerOpt-this.AIOpt]}`);
            this.gameLock = 0;
        },
        start: function() {
            if (this.gameLock) return; //当前游戏还在进行中
            this.gameLock = 1;
            this.getAIChoice();
            const timeId = setInterval(() => { //监听机器人的猜拳是否结束
                if (!this.transitionEnd) return; //机器人还在猜拳中
                this.transitionEnd = 0;
                clearInterval(timeId);
                this.winOrLose();
            }, handlerDis);
        }
    }
})